﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class Cost_UserControl_uc_NhapChiPhi : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            InitData();
        }
    }

    private void InitData()
    {

        btnAdd.Text = "Thêm mới";
        hdfID.Value = "-1";
        btnAdd.Visible = true;
        lblAlerting.Text = string.Empty;
        txtSoVanDon.Text = string.Empty;
        CostBO pur = new CostBO();
        List<ZD_PRC_ZD_COST_INPUT_GETALL_COSTResult> rs = new List<ZD_PRC_ZD_COST_INPUT_GETALL_COSTResult>();
        //Duong bien
        rs = pur.CostGetAll(1);
        gridDuongBien.DataSource = rs;
        gridDuongBien.DataBind();
        //Duong khong
        rs = pur.CostGetAll(2);
        gridDuongKhong.DataSource = rs;
        gridDuongKhong.DataBind();

        //Duong khac
        rs = pur.CostGetAll(3);
        gridDuongKhac.DataSource = rs;
        gridDuongKhac.DataBind();
        TinhTongChiPhi();

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CostBO pur = new CostBO();
            if (txtSoVanDon.Text.Trim().Length <= 0)
            {
                lblAlerting.Text = "Bạn chưa nhập số vận đơn!";
                return;
            }
            else
            {
                // Kiem tra xem so vận đơn này có chưa

                if (pur.CostInputCheckSoVanDon(txtSoVanDon.Text.Trim()) > 0)
                {
                    bool checkBien = false;
                    bool checkKhong = false;
                    bool checkKhac = false;
                    if ((gridDuongBien.Rows.Count <= 0) && (gridDuongKhong.Rows.Count <= 0) && (gridDuongKhac.Rows.Count <= 0))
                    {
                        lblAlerting.Text = "Không có dữ liệu!";
                        return;
                    }

                    if (gridDuongBien.Rows.Count > 0)
                    {
                        for (int i = 0; i < gridDuongBien.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongBien.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                checkBien = true;
                                break;


                            }
                        }


                    }

                    if (gridDuongKhong.Rows.Count > 0)
                    {
                        for (int i = 0; i < gridDuongKhong.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongKhong.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                checkKhong = true;
                                break;


                            }
                        }


                    }
                    if (gridDuongKhac.Rows.Count > 0)
                    {
                        for (int i = 0; i < gridDuongKhac.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongKhac.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                checkKhac = true;
                                break;


                            }
                        }


                    }
                    if ((!checkBien) && (!checkKhong) && (!checkKhac))
                    {
                        lblAlerting.Text = "Không có dữ liệu!";
                        return;
                    }

                    // lưu master
                    bool save = true;
                    ZD_COST_INPUT obj = new ZD_COST_INPUT();
                    ZD_COST_INPUT_BELONG objBelong = new ZD_COST_INPUT_BELONG();
                    obj.ID = int.Parse(hdfID.Value);
                    obj.SOVANDON = txtSoVanDon.Text.Trim();
                    obj.SOTOKHAI = txtSoToKhai.Text.Trim();
                    obj.CREATEUSER = int.Parse(Session["UserID"].ToString());
                    if (pur.Cost_Input_Update(obj))
                        if (int.Parse(hdfID.Value) > 0)
                        {
                            objBelong.MASTERID = int.Parse(hdfID.Value);
                            objBelong.CREATEUSER = int.Parse(Session["UserID"].ToString());
                            string dsChiPhiId = "";
                            string dsSOTIEN = "";
                            for (int i = 0; i < gridDuongBien.Rows.Count; i++)
                            {
                                // Cell 0 la checkbox
                                CheckBox chkSave = ((CheckBox)gridDuongBien.Rows[i].Cells[0].FindControl("chkSave"));
                                if (chkSave.Checked)
                                {
                                    try
                                    {
                                        dsChiPhiId += ((Label)gridDuongBien.Rows[i].Cells[1].FindControl("lblListingCHIPHID")).Text + "@";

                                    }
                                    catch
                                    {

                                    }
                                    try
                                    {
                                        dsSOTIEN += ((TextBox)gridDuongBien.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", "") + "@";

                                    }
                                    catch
                                    {
                                        dsSOTIEN += "0@";
                                    }




                                }
                            }

                            for (int i = 0; i < gridDuongKhong.Rows.Count; i++)
                            {
                                // Cell 0 la checkbox
                                CheckBox chkSave = ((CheckBox)gridDuongKhong.Rows[i].Cells[0].FindControl("chkSave"));
                                if (chkSave.Checked)
                                {
                                    try
                                    {
                                        dsChiPhiId += ((Label)gridDuongKhong.Rows[i].Cells[1].FindControl("lblListingCHIPHID")).Text + "@";

                                    }
                                    catch
                                    {

                                    }
                                    try
                                    {
                                        dsSOTIEN += ((TextBox)gridDuongKhong.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", "") + "@";

                                    }
                                    catch
                                    {
                                        dsSOTIEN += "0@";
                                    }




                                }
                            }

                            for (int i = 0; i < gridDuongKhac.Rows.Count; i++)
                            {
                                // Cell 0 la checkbox
                                CheckBox chkSave = ((CheckBox)gridDuongKhac.Rows[i].Cells[0].FindControl("chkSave"));
                                if (chkSave.Checked)
                                {
                                    try
                                    {
                                        dsChiPhiId += ((Label)gridDuongKhac.Rows[i].Cells[1].FindControl("lblListingCHIPHID")).Text + "@";

                                    }
                                    catch
                                    {

                                    }
                                    try
                                    {
                                        dsSOTIEN += ((TextBox)gridDuongKhac.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", "") + "@";

                                    }
                                    catch
                                    {
                                        dsSOTIEN += "0@";
                                    }




                                }
                            }
                            int rs = pur.Cost_Input_BelongUpdate(objBelong, dsChiPhiId, dsSOTIEN);
                            if (rs <= 0)
                            {
                                save = false;
                            }
                            if (!save)
                            {
                                lblAlerting.Text = "Cập nhật dữ liệu thất bại, bạn vui lòng thử lại sau!";
                                return;
                            }
                            else
                            {
                                lblAlerting.Text = "Bạn cập nhật dữ liệu thành công!";
                                btnAdd.Text = "Cập nhật";
                                return;
                            }
                        }
                        else
                        {
                            lblAlerting.Text = "Số vận đơn này đã được nhập!";
                            return;
                        }
                }
                else
                {
                    bool checkBien = false;
                    bool checkKhong = false;
                    bool checkKhac = false;
                    if ((gridDuongBien.Rows.Count <= 0) && (gridDuongKhong.Rows.Count <= 0) && (gridDuongKhac.Rows.Count <= 0))
                    {
                        lblAlerting.Text = "Không có dữ liệu!";
                        return;
                    }

                    if (gridDuongBien.Rows.Count > 0)
                    {
                        for (int i = 0; i < gridDuongBien.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongBien.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                checkBien = true;
                                break;


                            }
                        }


                    }

                    if (gridDuongKhong.Rows.Count > 0)
                    {
                        for (int i = 0; i < gridDuongKhong.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongKhong.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                checkKhong = true;
                                break;


                            }
                        }


                    }
                    if (gridDuongKhac.Rows.Count > 0)
                    {
                        for (int i = 0; i < gridDuongKhac.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongKhac.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                checkKhac = true;
                                break;


                            }
                        }


                    }
                    if ((!checkBien) && (!checkKhong) && (!checkKhac))
                    {
                        lblAlerting.Text = "Không có dữ liệu!";
                        return;
                    }
                    // lưu master
                    bool save = true;
                    ZD_COST_INPUT obj = new ZD_COST_INPUT();
                    ZD_COST_INPUT_BELONG objBelong = new ZD_COST_INPUT_BELONG();
                    obj.SOVANDON = txtSoVanDon.Text.Trim();
                    obj.SOTOKHAI = txtSoToKhai.Text.Trim();
                    obj.CREATEUSER = int.Parse(Session["UserID"].ToString());
                    int masterId = pur.CostInputInsert(obj);
                    hdfID.Value = masterId.ToString();
                    if (masterId > 0)
                    {
                        objBelong.MASTERID = masterId;
                        objBelong.CREATEUSER = int.Parse(Session["UserID"].ToString());
                        string dsChiPhiId = "";
                        string dsSOTIEN = "";
                        for (int i = 0; i < gridDuongBien.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongBien.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                try
                                {
                                    dsChiPhiId += ((Label)gridDuongBien.Rows[i].Cells[1].FindControl("lblListingCHIPHID")).Text + "@";

                                }
                                catch
                                {

                                }
                                try
                                {
                                    dsSOTIEN += ((TextBox)gridDuongBien.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", "") + "@";

                                }
                                catch
                                {
                                    dsSOTIEN += "0@";
                                }




                            }
                        }

                        for (int i = 0; i < gridDuongKhong.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongKhong.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                try
                                {
                                    dsChiPhiId += ((Label)gridDuongKhong.Rows[i].Cells[1].FindControl("lblListingCHIPHID")).Text + "@";

                                }
                                catch
                                {

                                }
                                try
                                {
                                    dsSOTIEN += ((TextBox)gridDuongKhong.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", "") + "@";

                                }
                                catch
                                {
                                    dsSOTIEN += "0@";
                                }




                            }
                        }

                        for (int i = 0; i < gridDuongKhac.Rows.Count; i++)
                        {
                            // Cell 0 la checkbox
                            CheckBox chkSave = ((CheckBox)gridDuongKhac.Rows[i].Cells[0].FindControl("chkSave"));
                            if (chkSave.Checked)
                            {
                                try
                                {
                                    dsChiPhiId += ((Label)gridDuongKhac.Rows[i].Cells[1].FindControl("lblListingCHIPHID")).Text + "@";

                                }
                                catch
                                {

                                }
                                try
                                {
                                    dsSOTIEN += ((TextBox)gridDuongKhac.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", "") + "@";

                                }
                                catch
                                {
                                    dsSOTIEN += "0@";
                                }




                            }
                        }
                        int rs = pur.Cost_Input_BelongInsert(objBelong, dsChiPhiId, dsSOTIEN);
                        if (rs <= 0)
                        {
                            save = false;
                        }
                        if (!save)
                        {
                            lblAlerting.Text = "Lưu dữ liệu thất bại!";
                            return;
                        }
                        else
                        {
                            lblAlerting.Text = "Lưu dữ liệu thành công!";
                            btnAdd.Text = "Cập nhật";
                            txtSoVanDon.ReadOnly = true;
                            return;
                        }
                    }
                    else
                    {
                        lblAlerting.Text = "Số vận đơn này đã được thêm mới!";
                        return;
                    }

                }

            }

        }

        catch (Exception ex)
        {


        }
    }
    private void TinhTongChiPhi()
    {
        int stPhiDuongBien = 0;
        int stPhiDuongKhong = 0;
        int stPhiDuongBo = 0;
        for (int i = 0; i < gridDuongBien.Rows.Count; i++)
        {
            // Cell 0 la checkbox

            CheckBox chkSave = ((CheckBox)gridDuongBien.Rows[i].Cells[0].FindControl("chkSave"));
            if (chkSave.Checked)
            {

                try
                {
                    stPhiDuongBien += int.Parse(((TextBox)gridDuongBien.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", ""));

                }
                catch
                {
                    stPhiDuongBien += 0;
                }

            }
        }

        for (int i = 0; i < gridDuongKhong.Rows.Count; i++)
        {
            // Cell 0 la checkbox
            CheckBox chkSave = ((CheckBox)gridDuongKhong.Rows[i].Cells[0].FindControl("chkSave"));
            if (chkSave.Checked)
            {
                try
                {
                    stPhiDuongKhong += int.Parse(((TextBox)gridDuongKhong.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", ""));

                }
                catch
                {
                    stPhiDuongKhong += 0;
                }

            }
        }

        for (int i = 0; i < gridDuongKhac.Rows.Count; i++)
        {
            // Cell 0 la checkbox
            CheckBox chkSave = ((CheckBox)gridDuongKhac.Rows[i].Cells[0].FindControl("chkSave"));
            if (chkSave.Checked)
            {
                try
                {
                    stPhiDuongBo += int.Parse(((TextBox)gridDuongKhac.Rows[i].Cells[4].FindControl("txtSOTIEN")).Text.Trim().Replace(",", ""));

                }
                catch
                {
                    stPhiDuongBo += 0;
                }

            }
        }
        if (stPhiDuongBien == 0)
        {
            lblTongCongDuongBien.Text = "0";
        }
        else
        {
            lblTongCongDuongBien.Text = string.Format("{0:0,0}", stPhiDuongBien);
        }
        if (stPhiDuongKhong == 0)
        {
            lblTongCongDuongKhong.Text = "0";
        }
        else
        {
            lblTongCongDuongKhong.Text = string.Format("{0:0,0}", stPhiDuongKhong);
        }
        if (stPhiDuongBo == 0)
        {
            lblTongCongDuongBo.Text = "0";
        }
        else
        {
            lblTongCongDuongBo.Text = string.Format("{0:0,0}", stPhiDuongBo);
        }

    }

}
